package com.nbjtjc.safe.service.emg.handle;

import cn.hutool.core.util.ObjectUtil;
import com.nbjtjc.safe.bean.Result;
import com.nbjtjc.safe.mapper.emg.handle.*;
import com.nbjtjc.safe.model.emg.handle.*;
import com.nbjtjc.safe.model.sys.SysUser;
import com.nbjtjc.safe.util.MyShiroUtils;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.beans.Transient;
import java.util.*;

@Service
public class EmgEventService {
    @Autowired
    private EmgEventMapper emgEventMapper;

    @Autowired
    private EmgHandleMapper emgHandleMapper;
    @Autowired
    private EmgHandlePublicMapper emgHandlePublicMapper;
    @Autowired
    private EmgHandleListPublicMapper emgHandleListPublicMapper;

    @Autowired
    private EmgHandleGoodsMapper emgHandleGoodsMapper;
    @Autowired
    private EmgHandleListMapper emgHandleListMapper;
    @Autowired
    private EmgHandlePublicGoodsMapper emgHandlePublicGoodsMapper;
    @Autowired
    private EmgHandlePublicService emgHandlePublicService;

    @Transactional(rollbackFor = {Throwable.class})
    public Result add(EmgEvent emgEvent) throws Exception {
        Result result = new Result();
        Map<String, Object> data = new HashMap<>();
        if (ObjectUtil.isNull(emgEvent) || ObjectUtil.isNull(emgEvent.getTypeId())
                || ObjectUtil.isNull(emgEvent.getEventTime())) {
            result.setFalse("不能为空");
            return result;
        }
//        computeLevel(emgEvent);
        emgEvent.setCreateTime(new Date());
        emgEvent.setDeptId(MyShiroUtils.getDeptId());
        emgEvent.setCompanyId(MyShiroUtils.getCompanyId());
        emgEventMapper.insertSelective(emgEvent);
        data.put("handlePublic", addEventHandle(emgEvent));
        data.put("event", emgEvent);
        result.setData(data);
        return result;
    }

    public Result countEventLevel(EmgEvent emgEvent) {
        Result result = new Result();
        computeLevel(emgEvent);
        result.setData(emgEvent);
        return result;
    }

    public Result update(EmgEvent emgEvent) {
        Result result = new Result();
        if (ObjectUtil.isNull(emgEvent) || ObjectUtil.isNull(emgEvent.getId())) {
            result.setFalse("不能为空");
            return result;
        }
        computeLevel(emgEvent);
        emgEventMapper.updateByPrimaryKeySelective(emgEvent);
        result.setData(emgEvent);
        return result;
    }

    public Result delete(EmgEvent emgEvent) {
        Result result = new Result();
        emgEventMapper.deleteByPrimaryKey(emgEvent.getId());
        return result;
    }

    /**
     * 计算事件等级
     *
     * @param emgEvent
     */
    private void computeLevel(EmgEvent emgEvent) {
        int level = 0;
        int temp = computeDeathLevel(emgEvent);
        if (level < temp) {
            level = temp;
        }
        temp = computeInjuriesLevel(emgEvent);
        if (level < temp) {
            level = temp;
        }
        if (ObjectUtil.isNotNull(emgEvent.getInterruptTimeUnit())) {
            if (emgEvent.getInterruptType().equals("全幅")) {
                temp = computeAllInterruptLevel(emgEvent);
            } else {
                temp = computeSingleInterruptLevel(emgEvent);
            }
            if (level < temp) {
                level = temp;
            }
        }
        emgEvent.setEventLevel(level + "");
    }

    /**
     * 计算受伤的对应等级
     *
     * @param emgEvent
     * @return
     */
    private int computeInjuriesLevel(EmgEvent emgEvent) {
        if (ObjectUtil.isNull(emgEvent.getInjuriesNum())) {
            return 0;
        }
        if (emgEvent.getInjuriesNum() < 3) {
            return 4;
        }
        if (emgEvent.getInjuriesNum() < 10 && emgEvent.getInjuriesNum() >= 3) {
            return 3;
        }
        if (emgEvent.getInjuriesNum() < 50 && emgEvent.getInjuriesNum() >= 10) {
            return 2;
        }
        if (emgEvent.getInjuriesNum() >= 50) {
            return 1;
        }
        return 0;
    }

    /**
     * 计算死亡的对应等级
     *
     * @param emgEvent
     * @return
     */
    private int computeDeathLevel(EmgEvent emgEvent) {
        if (ObjectUtil.isNull(emgEvent.getDeathNum())) {
            return 0;
        }
        if (emgEvent.getDeathNum() == 0) {
            return 4;
        }
        if (emgEvent.getDeathNum() < 3 && emgEvent.getDeathNum() >= 1) {
            return 3;
        }
        if (emgEvent.getDeathNum() < 10 && emgEvent.getDeathNum() >= 4) {
            return 2;
        }
        if (emgEvent.getDeathNum() >= 10) {
            return 1;
        }
        return 0;
    }

    /**
     * 计算全幅封道对应的级别
     *
     * @param emgEvent
     * @return
     */
    private int computeAllInterruptLevel(EmgEvent emgEvent) {
        if (ObjectUtil.isNull(emgEvent.getInterruptTime())) {
            return 0;
        }
        if (emgEvent.getInterruptTime() >= 1 && emgEvent.getInterruptTime() < 6) {
            return 4;
        }
        if (emgEvent.getInterruptTime() >= 6 && emgEvent.getInterruptTime() < 12) {
            return 3;
        }
        if (emgEvent.getInterruptTime() >= 12 && emgEvent.getInterruptTime() < 24) {
            return 2;
        }
        if (emgEvent.getInterruptTime() >= 24) {
            return 1;
        }
        return 0;
    }

    /**
     * 计算单幅封道对应的级别
     *
     * @param emgEvent
     * @return
     */
    private int computeSingleInterruptLevel(EmgEvent emgEvent) {
        if (ObjectUtil.isNull(emgEvent.getInterruptTime())) {
            return 0;
        }
        if (emgEvent.getInterruptTime() >= 1 && emgEvent.getInterruptTime() < 3) {
            return 4;
        }
        if (emgEvent.getInterruptTime() >= 3) {
            return 3;
        }
        return 0;
    }

    private EmgHandlePublic addEventHandle(EmgEvent emgEvent) throws Exception {
        EmgHandlePublic emgHandlePublic = new EmgHandlePublic();
        EmgHandle emgHandle = emgHandleMapper.selectByPrimaryKey(emgEvent.getHandleId());
        emgHandlePublic.setName(emgHandle.getName());
        emgHandlePublic.setTypeId(emgHandle.getTypeId());
        emgHandlePublic.setTopTypeId(emgHandle.getTopTypeId());
        emgHandlePublic.setDeptId(MyShiroUtils.getDeptId());
        emgHandlePublic.setHandleId(emgEvent.getHandleId());
        emgHandlePublic.setEventId(emgEvent.getId());
        int res = 0;
        emgHandlePublic.setAttentionItem(emgHandle.getRemark());
        res = emgHandlePublicMapper.insertSelective(emgHandlePublic);
        if (res == 0) {
            throw new Exception("新增处置失败");
        } else {
            //新增成功的时候插入物资和handleList
            emgHandlePublicService.initHandleList(emgHandlePublic.getHandleId(), emgHandlePublic.getId());
        }
        return emgHandlePublic;
    }
}
